// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace eee.Sheffield.PZ.Imaging
{
    /// <summary>
    /// scanline field S
    /// </summary>
    public class ScanlineField
    {
        public int count = 0;
        public List<PZScanline> slArray = new List<PZScanline>();

        public ScanlineField()
        {
        }

        /// <summary>
        /// initialize center point field from track result
        /// </summary>
        /// <param name="tr"></param>
        public void InitFromCenterPointField(CenterPointField cpf)
        {
            count = cpf.count;

            for (int i = 0; i < cpf.count; i++)
            {
                int widthOfScanLine = Convert.ToInt32(cpf.rArray[i] * 6);
                int sampleRate = 1;
                PZPoint centrePoint = cpf.cpArray[i];
                double c = widthOfScanLine / 2;
                double r = cpf.rArray[i];
                double mu = cpf.muArray[i];
                double B = cpf.BArray[i];
                PZScanline currentSL = new PZScanline(widthOfScanLine, sampleRate, c, r, mu, B);
                currentSL.CalculateMin();   // ffit
                PZDirection sd = new PZDirection(cpf.vdArray[i]);
                sd.GetNormalDirection();

                // get scanline positions
                int halfwidth = widthOfScanLine / 2;
                for (int j = 0; j < widthOfScanLine; j++)
                {
                    double t = (double)j / (double)sampleRate;
                    PZPoint tempPoint = new PZPoint();
                    tempPoint.x = centrePoint.x + (t - halfwidth) * sd.x;
                    tempPoint.y = centrePoint.y + (t - halfwidth) * sd.y;
                    currentSL._fp[j] = new PZPoint(tempPoint.x, tempPoint.y);
                }
                slArray.Add(currentSL);
            }
        }

        /// <summary>
        /// write to file
        /// </summary>
        /// <param name="filename"></param>
        public void Write(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
            StreamWriter w = new StreamWriter(fs);
            // TODO write scanline field to file
            w.Close();
            fs.Close();
        } // Write()

        /// <summary>
        /// read from file
        /// </summary>
        /// <param name="filename"></param>
        public void Read(string filename)
        {
            // TODO read scanline field from file
        } // Read()
    }
}
